package cn.edu.sgu.www.mhxysy.controller.chongwu;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuInitSkill;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuSkillCategory;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuInitSkillService;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuSkillCategoryService;
import cn.edu.sgu.www.mhxysy.vo.chongwu.ChongwuInitSkillVO;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宠物初始技能管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chongwu_init_skill", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChongwuInitSkillController {

	private final ChongwuInitSkillService chongwuInitSkillService;
	private final ChongwuSkillCategoryService chongwuSkillCategoryService;

	@Autowired
	public ChongwuInitSkillController(
			ChongwuInitSkillService chongwuInitSkillService,
			ChongwuSkillCategoryService chongwuSkillCategoryService) {
		this.chongwuInitSkillService = chongwuInitSkillService;
		this.chongwuSkillCategoryService = chongwuSkillCategoryService;
	}

	@ApiOperation("保存宠物初始技能")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public JsonResult<Void> save(
			@ApiParam(name="list", value = "宠物初始技能列表", required = true)
			@RequestParam(name = "list", required = true)
			List<ChongwuInitSkill> list) {
		chongwuInitSkillService.save(list);

		return JsonResult.success(CustomMessages.SAVE_SUCCESS);
	}

	@ApiOperation("查询宠物初始技能列表")
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public JsonResult<List<ChongwuInitSkill>> selectList(
			@ApiParam(name="chongwuId", value = "宠物类型ID", required = false)
			@NotNull(message = CustomMessages.CHONGWU_CATEGORY_ID_NOT_NULL) Integer chongwuId) {
		List<ChongwuInitSkill> list = chongwuInitSkillService.selectByChongwuId(chongwuId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询宠物初始技能列表")
	@RequestMapping(value = "/selectByChongwuId", method = RequestMethod.GET)
	public JsonResult<List<ChongwuInitSkillVO>> selectByChongwuId(
			@ApiParam(name="chongwuId", value = "宠物类型ID", required = true)
			@NotNull(message = CustomMessages.CHONGWU_CATEGORY_ID_NOT_NULL) Integer chongwuId) {
		List<ChongwuInitSkill> list = chongwuInitSkillService.selectByChongwuId(chongwuId);
		List<ChongwuInitSkillVO> result = new ArrayList<>();

		for (ChongwuInitSkill initSkill: list) {
			// 通过ID查询宠物技能类型
			ChongwuSkillCategory category = chongwuSkillCategoryService.selectById(initSkill.getSkillId());

			ChongwuInitSkillVO skillVO = new ChongwuInitSkillVO();

			// 设置属性值
			skillVO.setTag(initSkill.getTag());
			skillVO.setName(category.getName());
			skillVO.setType(category.getType());
			skillVO.setImage(category.getImage());
			skillVO.setVerifiable(category.getVerifiable());

			result.add(skillVO);
		}

		return JsonResult.success(null, result);
	}

}